<?php
	include_once("phpConfigulation/DBConfig.php");	
	class ConnectDB
	{
		var $connection;
		function OpenConnection(){
			$this->connection = mysqli_connect(host,user,password) or die('Could not connect: ' . mysql_error());
			mysqli_select_db($this->connection ,dbName) or die("Error");
			mysqli_query($this->connection,"SET NAMES TIS620") or die("Error");
		}
		
		function ExecuteQuery($sql){			
			$query = mysqli_query($this->connection,$sql) or die('Error: ' . mysqli_error());
			return $query;
		}

		
		function ExecuteNonQuery($sql){
			$query = mysqli_query($this->connection,$sql) or die('Error: ' . mysqli_error());
			return $query;
		}

		function InsertGetID(){
			return mysqli_insert_id() or die('Error: ' . mysqli_error());
		}

		function ExecuteQueryArray($sql){
			$query = $this->ExecuteQuery($sql);
			$data = mysqli_fetch_array($query,MYSQL_BOTH);
			return $data;
		}
		
		function ExecuteScalar($sql){
			$data = $this->ExecuteQueryArray($sql);
			return $data[0];
		}
		
		function CloseConnection(){	
			if(ISSET($this->connection)){
				mysqli_close($this->connection) or die('close connection error: ' . mysqli_error());
			}
		}
		
		function ExecuteQueryDTO($SQL,$DTO)
		{
			$this->OpenConnection();
			$rows = $this->ExecuteQuery($SQL);
			$lstDTO = array();
			
			$class_name = get_class($DTO);
			$class_vars = get_class_vars($class_name);
			while($row = mysqli_fetch_array($rows,MYSQL_BOTH)){
				$DTOTemp = new $class_name();
				foreach ($class_vars as $name => $value) {
					$DTOTemp->$name = $row[$name];
				}
				array_push($lstDTO,$DTOTemp);
			}
			
			$this->CloseConnection();
			return $lstDTO;
		}
		
			
		function UpdateEntity($TableName, $ColumnPKName, $DTO)
		{
			$this->OpenConnection();			
			$class_name = get_class($DTO);
			$class_vars = get_class_vars($class_name);
			$PKValue = "";
			$sql = "UPDATE $TableName SET ";
			foreach ($class_vars as $name => $value) {
				$DTOValue = $DTO->$name;
				$sql .= " $name = '$DTOValue',";
				if($ColumnPKName == $name){ $PKValue = $DTOValue; }
			}
			
			$sql = substr($sql,0,strlen($sql)-1);

			$sql.= " WHERE $ColumnPKName = '$PKValue'";
			$rows = $this->ExecuteNonQuery($sql);
			$this->CloseConnection();
		}
		
		function InsertEntity($TableName, $ColumnPKName, $DTO)
		{
			$this->OpenConnection();			
			$class_name = get_class($DTO);
			$class_vars = get_class_vars($class_name);
			$PKValue = "";
			$sql = "INSERT INTO $TableName (";
			$sqlValue = "VALUES (";
			foreach ($class_vars as $name => $value) {
				if($ColumnPKName != $name){  
					$DTOValue = $DTO->$name;
					$sql .= "$name,";
					$sqlValue .= "'$DTOValue',";
				}
			}
			
			$sqlValue = (substr($sqlValue,0,strlen($sqlValue)-1) . ")");			
			$sql = (substr($sql,0,strlen($sql)-1) . ")");

			$sql.= " $sqlValue";
			$rows = $this->ExecuteNonQuery($sql);
			$this->CloseConnection();
		}
		
		function Delete($TableName,$ColumnPKName, $Value)
		{
			$this->OpenConnection();
			$sql = "DELETE FROM $TableName WHERE $ColumnPKName ='$Value'";
			$this->ExecuteNonQuery($sql);
			$this->CloseConnection();
		}
	}
?>